
$.ajax({ url: "http://192.168.31.88:8082/monitoringapi/fetchwebsocketurl",type:'post', success: function(res){
		console.log(res);
		if(res.code==0){
			contactSocket(res.data);
		}else {
			alert(res.msg)
		}
	}});


function contactSocket(url){

// Let the library know where WebSocketMain.swf is:
	WEB_SOCKET_SWF_LOCATION = "./WebSocketMain.swf";
// Write your code in the same way as for native WebSocket:
	var ws = new WebSocket(url);
	ws.onerror = function(event) {
		$(".maskwrap h1").css('animation-timing-function','forwards');
		$("h1::before").css('animation','none');
		$("h1::after").css('animation','none');
		$(".sockeetTip").addClass('text-danger');
		$(".sockeetTip").text('连接失败！');
	};
	switch (ws.readyState) {
		case WebSocket.CONNECTING:
			// do something
			break;
		case WebSocket.OPEN:
			$(".sockeetTip").text('连接成功！')
			break;
		case WebSocket.CLOSING:

			$(".sockeetTip").text('连接关闭中！')
			// do something
			break;
		case WebSocket.CLOSED:
			// do something
			$(".sockeetTip").text('连接已关闭！')
			break;
		default:
			// this never happens
			break;
	}
	ws.onopen = function() {
		// ws.send("Hello"); // Sends a message.
		setTimeout(function() {
			$('.maskwrap').slideUp()
		}, 1000)
	};
	ws.onmessage = function(evt) {
			if (/^\d{4}$/.test(evt.data.slice(0, 4))) {
				var time = evt.data.slice(0, 24);
				var content = evt.data.slice(24);
				var itemStr = '<div class="logItem"><span class="text-muted">' + time + '</span>' + content + '</div>'
				if (itemStr.indexOf('INFO') != -1) {
					var itemStr = itemStr.replace('INFO', '<span class="text-info">$&</span>');
				} else if (itemStr.indexOf('ERROR') != -1) {
					var itemStr = itemStr.replace('ERROR', '<span class="text-danger">$&</span>');
				} else if (itemStr.indexOf('WARN ') != -1) {
					var itemStr = itemStr.replace('WARN ', '<span class="text-warning">$&</span>');
				} else if (itemStr.indexOf('DEBUG ') != -1) {
					var itemStr = itemStr.replace('DEBUG ', '<span class="text-success">$&</span>');
				}
			} else {
				var itemStr = '<div class="logItem"><span class="text-muted	">' + evt.data + '</span></div>'
			}

			var item = $(itemStr);
			$('#logWrap').append(item);
			setTimeout(function() {
				//设置滚动条到最底部
				var h = $(document).height() - $(window).height();
				$(document).scrollTop(h);
			}, 300);
		if (evt.data.indexOf('Monitoring Connection Exception')==-1){
		}else {
			var itemStr = '<div class="logItem"><span class="text-error">服务器错误！已关闭链接。</span></div>';

			var item = $(itemStr);
			$('#logWrap').append(item);
			ws.close();
		}


	};

}